package ThreadDemo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2025-03-20
 * Time: 11:03
 */
public class ThreadDemo10 {
    public static void main(String[] args) {
        Thread t = new Thread(
                ()->{
                    while (!Thread.currentThread().isInterrupted()){
                        //Thread.currentThread()获取当前线程对象的引用
                        //isInterrupted判断当前线程的中断状态，中断状态为true，则表示线程中断，方法返回值true
                        //否则返回false
                        System.out.println("我是一个线程，正在工作中...");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            //break;//直接跳出循环
                            Thread.currentThread().interrupt();//手动设置当前线程的中断状态为true
                        }
                    }
                    System.out.println("线程执行完毕！");
                }
        );

        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //使用interrupt方法，来修改标志位的值
        System.out.println("让t线程结束");
        t.interrupt();//将线程中断状态设为true，相当于将isQuit=true
    }
}
